import requests
from lxml import etree

url = "https://movie.douban.com/top250?start=0&filter="

headers = {
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0'
}


def get_first_text(list):
    try:
        return list[0].strip()
    except:
        return ""

urls=['https://movie.douban.com/top250?start={}&filter='.format(str(i*25)) for i in range(10)]
count=1
for url in urls:
    print(url)
    res=requests.get(url=url,headers=headers)
    print(res.status_code)
    html=etree.HTML(res.text)
    list=html.xpath('//*[@id="content"]/div/div[1]/ol/li')



    print(len(list))

    for li in list:
        title=get_first_text(li.xpath('./div/div[2]/div[1]/a/span[1]/text()'))
        src=get_first_text(li.xpath('./div/div[2]/div[1]/a/@href'))
        director_and_cast=get_first_text(li.xpath('./div/div[2]/div[2]/p[1]/text()'))
        score=get_first_text(li.xpath('./div/div[2]/div[2]/div/span[2]/text()'))
        comment=get_first_text(li.xpath('./div/div[2]/div[2]/div/span[4]/text()'))
        summary_list = li.xpath('./div/div[2]/div[2]/p[2]/span/text()')
        summary = summary_list[0].strip() if summary_list else ""
        print(count,title,src,director_and_cast,score,comment,summary)
        count+=1